package cn.bob.basic.controller;

import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.FastdfsUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

/**
 * @author bob
 * @data 2022/10/15-21:36
 */
@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {
    //上传
    @PostMapping
    public AjaxResult upload(@RequestPart("file") MultipartFile file) throws IOException {
        //获取真实名称
        String originalFilename = file.getOriginalFilename();
        //获取扩展名
        String extension = FilenameUtils.getExtension(originalFilename);
        String address = FastdfsUtil.upload(file.getBytes(), extension);
        return AjaxResult.me().success(address);
    }


    //删除
    @DeleteMapping
    public AjaxResult delete(@RequestParam("filePath") String filePath){
        //前端传回的数据/group1/M00/02/09/CgAIC2NKtGWAcUB_AAIbh_fh03I014.jpg
        //从下标1开始切group1/M00/02/09/CgAIC2NKtGWAcUB_AAIbh_fh03I014.jpg
        if (filePath.contains("http://123.207.27.208")){
            filePath=filePath.substring(21);
        }
        String path1 = filePath.substring(1);
        //从0取到第一个/的下标 group1  获取到组名
        String groudname = path1.substring(0, path1.indexOf("/"));
        //获取到图片路径M00/02/09/CgAIC2NKtGWAcUB_AAIbh_fh03I014.jpg
        String picPath = path1.substring(path1.indexOf("/") + 1);
        //删除
        FastdfsUtil.delete(groudname,picPath);
        return  AjaxResult.me().success();
    };
}
